package com.zw.mes.module.tecdrawsign.service;

import com.zw.mes.entity.technology.DrawSign;
import com.zw.mes.entity.technology.DsnCoatSheet;
import com.zw.mes.entity.technology.ReceiveSum;
import com.zw.mes.module.system.service.LogService;
import com.zw.mes.module.tecdrawsign.mapper.PaintingMapper;
import com.zw.mes.module.technology.repository.ReceiveSumRepository;
import com.zw.mes.util.ShiroUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author mayongfeng
 * @version 1.0.0
 * @ClassName PaintingService.java
 * @Description TODO
 * @createTime 2021年05月13日 15:36:00
 */
@Service
@SuppressWarnings("all")
public class PaintingService {

    @Autowired
    private PaintingMapper paintingMapper;

    @Autowired
    private LogService logService;

    @Autowired
    private ReceiveSumRepository receiveSumRepository;
    /**
     * 查询所有审核过后的涂装设计清单
     *
     * @param coatId
     * @return
     */
    public List<DsnCoatSheet> getAll(DsnCoatSheet dsnCoatSheet) {

        return paintingMapper.getAll(dsnCoatSheet);
    }

    /**
     * 根据ID 取得涂装设计清单
     *
     * @param coatId
     * @return
     */
    public DsnCoatSheet findDsnCoatById(Integer coatId) {

        return paintingMapper.findById(coatId);
    }

    /** 查询涂装设计原始单数据*/
    public List<DsnCoatSheet> findCoatDetailBySheetId(Integer coatId) {
        return paintingMapper.findCoatDetailBySheetId(coatId);
    }

    /** 查询涂装设计变更单数据*/
    public List<DsnCoatSheet> findCoatDetailChangeBySheetId(Integer coatId) {
        return paintingMapper.findCoatDetailChangeBySheetId(coatId);
    }

    /** 接收涂装设计清单*/
    public int receiveCoat(DsnCoatSheet dsnCoatSheet) {

        logService.saveLog("接收涂装设计清单", "涂装ID：" + dsnCoatSheet.getId());

        /** 首次接收时在接收记录表里面添加记录信息*/
        ReceiveSum str = receiveSumRepository.findByProCodeAndPartAndSpecial(dsnCoatSheet.getProCode(),dsnCoatSheet.getProPart(),dsnCoatSheet.getSpecial());
        /** 判断集合为不为null*/
        if(null == str || "".equals(str)){
            /** 如果记录表里面为空就向记录表里面添加接收记录*/
            ReceiveSum receiveSum = new ReceiveSum();
            receiveSum.setProCode(dsnCoatSheet.getProCode());
            receiveSum.setProName(dsnCoatSheet.getProName());
            receiveSum.setProNum(dsnCoatSheet.getProNum());
            receiveSum.setPart(dsnCoatSheet.getProPart());
            receiveSum.setSpecial(dsnCoatSheet.getSpecial());
            receiveSum.setOutStatus(0);
            receiveSum.setCardStatus(0);
            receiveSum.setMakeStatus(0);
            receiveSum.setPaintStatus(1);
            receiveSumRepository.save(receiveSum);
        }else if (str.getPaintStatus()==0){
            str.setPaintStatus(1);
            receiveSumRepository.save(str);
        }

        String name = ShiroUtils.getUser().getName();
        Date ptime = new Date();
        return paintingMapper.receiveCoat(dsnCoatSheet.getId(), name,ptime);
    }

    public DsnCoatSheet getDsnCoatSheetByid(Integer id) {
        return  paintingMapper.getDsnCoatSheetByid(id);
    }

    public DsnCoatSheet getDsnCoatSheetChangeByid(Integer id) {
        return  paintingMapper.getDsnCoatSheetChangeByid(id);
    }

    public int selectPaintingCount() {
        return  paintingMapper.selectPaintingCount();
    }
}
